﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Chapter28
{
    using System.Threading;

    static partial class Program
    {
        internal static void Begin(Action action, bool active = false)
        {
            if (active)
            {
                Console.WriteLine("开始演示：");
                action();
                Console.WriteLine("演示完毕：");
                Console.ReadLine();
            }
        }

        private static class SomeType
        {
            public static int x = 0;
        }

        internal static void C03()
        {
            SomeType.x = 0x01234567;
            StrangeBehavior.Go();
        }

        private static class StrangeBehavior
        {
            private static bool stopWorker = false;

            public static void Go()
            {
                Console.WriteLine("Main: letting worker run for 5 seconds");
                var t = new Thread(Worker);
                t.Start();
                Thread.Sleep(5000);
                stopWorker = true;
                Console.WriteLine("Main: waitin for worker to stop");
                t.Join();
            }

            private static void Worker(object state)
            {
                int x = 0;
                while (!stopWorker)
                    x++;
                Console.WriteLine("Worker: Stopped when x={0}", x);
            }
        }
    }
}